package com.sinoglobal.dumping.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.base.Dumpling_SinoBaseActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;

/**
 * Created by lxy on 2015/11/12.
 */
public class Dumpling_VoteActivity extends Dumpling_SinoBaseActivity {

    private WebView mWebView;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        View view = getView(R.id.title_right_btn_share);
        hideView(mTemplateRightText, mTemplateRightImg, view);
        mTemplateTitleText.setText("明星投票");
        setContentView(R.layout.dumpling_activity_actionweb);
        String url = (String) getIntent().getExtras().get("url");
        initView();
        initView();

        WebSettings webSettings = mWebView.getSettings();
        // 设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        // 设置可以访问文件
        webSettings.setAllowFileAccess(true);
        // 设置支持缩放
        webSettings.setBuiltInZoomControls(true);
        // 滚动条风格，0是不占用额外位置，覆盖到当前网页上
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        // 加载需要显示的网页
        mWebView.loadUrl(url);
        // 设置Web视图
        mWebView.setWebViewClient(new webViewClient());

        mWebView.setWebChromeClient(new WebChromeClient());

    }

    private void initView() {
        mWebView = getView(R.id.actionweb_web);
        progressBar = getView(R.id.actionweb_pro);
    }

    @Override
    public void onAntiViolence(View view) {

    }

    public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressBar.setVisibility(View.GONE);
            } else {
                if (progressBar.getVisibility() == View.GONE)
                    progressBar.setVisibility(View.VISIBLE);
                progressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    @Override
    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        }
        finish();// 结束退出程序
        return false;
    }

    // Web视图
    private class webViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    protected void onDestroy() {
        mWebView.destroy();
        super.onDestroy();
    }
}
